'''
Created on April 7, 2011

@author: yc30v1m
'''
from glob import glob
import os
from library.AutoLoad import AutoLoad
from logging import getLogger
import string


log = getLogger(__name__)

class Excel(object):
    '''
    classdocs
    '''
    _confReader = ""
    _testCases = {}
    _testCaseIdHeaderList = ["TESTCASE_ID", "TCID"]
    _dataEncoding = "UTF-8"
    __fileExt__ = 'xls'

    def __init__(self,setUpInfo):
        try:
            self._confReader = AutoLoad.ClassAsObject('library.Configuration')
            
        except:
            log.critical("Exception occured")
            AutoLoad.logExceptionInfo()
        
    def getTestCases(self):
        log.debug("Getting TestCases from Excel Reader")
        try:
            self.__readAllExcel()
            return self._testCases
        except:
            log.critical("Unable to read Testcases from Excel Reader")
        return {}
        
        
    def __readAllExcel(self):
        excelLocation = self._confReader.getOptionFromSection('testcases_excel', 'testcases_location')
        excelLocation = excelLocation + os.sep
        #self._reader = AutoLoad.ClassAsObject('plugins.Excel.Reader')getOptionFromSection
        log.debug("Reading Test Cases from : " + excelLocation)
        excelFiles = glob(excelLocation + '*.' + self.__fileExt__)
        if len(excelFiles) > 0:
            for excelFile in excelFiles:
                try:
                    self.__readOneExcel(excelFile)
                except:
                    log.warning("Unable to read Excel" + str(excelFile))
                    AutoLoad.logExceptionInfo()
                
            
    def __readOneExcel(self,FilePath):
        try:
            readClass = AutoLoad.ClassOnly('plugins.Excel.Reader')
            self._reader = readClass(FilePath)
            excelsheet = self._reader.sheet("Testcases$", self._dataEncoding)
            for row in excelsheet.paged():
                if str(row['RUN_TEST']).upper()=="YES":
                    self._setTestCasesGenericFormat(row)

        except:
            log.warning("Exception Occured")
            AutoLoad.logExceptionInfo()
            
    def _setTestCasesGenericFormat(self, excelTestCase={}):
        try:
            tc = {}
            tcid = ""
            if len(excelTestCase) > 0:
                for key in excelTestCase.keys():
                    try:
                        for tcidCol in self._testCaseIdHeaderList:
                            if key == tcidCol:
                                tcid = excelTestCase.get(key)
                    except:
                        pass # Dirty way 
                    tc[key] = excelTestCase.get(key)
                if tcid != "":
                    self._testCases[tcid] = tc
                else:
                    log.warning("TC Not Loaded" + str(excelTestCase))
        except:
            log.warning("TestCases not in proper format " + str(tc))
            AutoLoad.logExceptionInfo()
                    
        
        